package com.huatai.bpmn.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.bpmn.commom.emnu.DicCode;
import com.huatai.bpmn.commom.emnu.StatusCode;
import com.huatai.bpmn.entity.DicEntity;
import com.huatai.bpmn.entity.DicEntity;
import com.huatai.bpmn.entity.UserEntity;
import com.huatai.bpmn.service.DicService;
import com.huatai.bpmn.service.UserService;
import com.huatai.common.api.R;
import com.huatai.common.util.StringUtil;
import com.huatai.user.dubbo.UserDubboApi;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  字典表
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@RestController
@RequestMapping("/dic")
@Api(value = "字典表", tags = "字典表")
public class DicController {

	@Autowired
	private DicService dicService;
	@Autowired
	private UserDubboApi userDubboApi;

	@ApiOperation("字典查询--表头管理字段类型下拉列表,表格类型id为41")
	@GetMapping("/dataTypeList")
	public R dataTypeList(){
		List<DicEntity> list = dicService.list(DicCode.dataType.getCode());
		return R.data(list);
	}
	@ApiOperation("字典新增")
	@PostMapping("/add")
	public R add(@RequestBody DicEntity date){
		//如果不选择父节点，父节点就默认为0
		if (StringUtil.isEmpty(date.getPid())){
			date.setPid("0");
		}
		boolean flag = dicService.save(date);
		return flag? R.success("新增成功"): R.fail("新增失败");
	}

	@ApiOperation("字典查询--表头管理下拉选项列表,表格类型id为0")
	@GetMapping("/selectList")
	public R selectList(){
		List<DicEntity> list = dicService.list(DicCode.parentType.getCode());
		return R.data(list);
	}

	@PostMapping(value = "/update")
	@ApiOperation("字典修改")
	public R update(@Valid @RequestBody DicEntity data) {
		//如果pid为空时默认为0
		if (StringUtil.isEmpty(data.getPid())){
			data.setPid("0");
		}
		boolean flag = dicService.updateById(data);
		return flag? R.success("修改成功"): R.fail("修改失败");
	}

	@DeleteMapping("/remove/{id}")
	@ApiOperation("字典删除")
	public R remove(@PathVariable("id") String id) {
		List<DicEntity> list = dicService.list(new LambdaQueryWrapper<>(DicEntity.class).eq(DicEntity::getPid, id));
		if (list.size()>0){
			return R.fail("存在子节点，无法删除");
		}
		boolean flag = dicService.removeById(id);
		return flag? R.success("删除成功"): R.fail("删除失败");
	}

	@ApiOperation("获取字典树形结构")
	@GetMapping("/treeList")
	public R treeList(){
		return R.data(dicService.treeList());
	}


	@ApiOperation("字典查询--表单外部参数列表")
	@GetMapping("/formExternalParameterList")
	public R formExternalParameterList(){
		List<DicEntity> list = dicService.list(DicCode.formExternalParameterType.getCode());
		return R.data(list);
	}

	@ApiOperation("字典查询--表单外部参数列表")
	@GetMapping("/selectPid")
	public R selectPid(@RequestParam String pid){
		List<DicEntity> list = dicService.list(new LambdaQueryWrapper<>(DicEntity.class).eq(DicEntity::getPid,pid));
		return R.data(list);
	}

	@ApiOperation("字典查询--根据pid查询数据")
	@GetMapping("/listPid")
	public R listPid(@RequestParam String pid){
		List<JSONObject> list = dicService.findChildrenData(pid);
		return R.data(list);
	}

	@GetMapping("/userList")
	@ApiOperation("用户列表")
	public R userList(){

		return R.data(userDubboApi.getList());
	}
}

